package ro.msg.office.ticketmanager.back.bean;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import ro.msg.office.entities.Department;
import ro.msg.office.entities.User;
import ro.msg.office.ticketmanager.DO.UserDO;
import ro.msg.office.ticketmanager.back.ifc.DepartmentManager;
import ro.msg.office.ticketmanager.back.ifc.TicketManager;
import ro.msg.office.ticketmanager.back.ifc.UserManager;

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)

public class UserManagerBean implements UserManager {

	
	@PersistenceContext(unitName="TM_PU")	
	private  EntityManager em;
	@EJB
	private DepartmentManager departmentEJB;
	@EJB
	private TicketManager ticketEJB;
	@EJB
	private UserManager userEJB;
	
	Logger logger = Logger.getLogger(UserManagerBean.class);

	public void createUser(String username, String password, long userType,String name, String departmentName) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setUserType(userType);
		user.setName(name);
		
		Department department = departmentEJB.findDepartmentByName(departmentName);
		user.setDepartment(department);
		
		em.persist(user);
		logger.info("User created with info: " + user.getUserId() + " " + user.getName());
	}
	
	/**
	 * Get all Users
	 * @param
	 * @return List<User>
	 */
	@SuppressWarnings("unchecked")
	public List<User> getUsers() {
		Query query = em.createNamedQuery("User.findAll");		
		List<User> results = query.getResultList();
		return results;
	}

	/**
	 * Find UserDo by user id
	 * @param id
	 * @return UserDO
	 */
	public UserDO findDOUserById(Long id){
		return mapUserToDO(em.find(User.class, id));
	}
	
	/**
	 * Find User by username and password
	 * @param username, password
	 * @return User
	 */
	public User findUserByNameAndPassword(String userName, String userPassword){
		Query query = em.createNamedQuery("User.findByNameAndPassword");
		query.setParameter("name", userName);
		query.setParameter("password", userPassword);
		if(query.getResultList().isEmpty()) return null;
		else{
			List<User> users = query.getResultList();
			return users.get(0);
		}
	}
	
	/**
	 * Check the user by userDO.username and userDO.password
	 * @param userDO
	 * @return UserDO
	 */	
	public UserDO check(UserDO userDO) {
		User user = findUserByNameAndPassword(userDO.getUsername(),userDO.getPassword());		
		if(user != null) return userEJB.mapUserToDO(user);
		else return userDO;
	}
	
	/**
	 * Map User to UserDO 
	 * @param User
	 * @return UserDO
	 */
	public UserDO mapUserToDO(User user){
		if(user != null){
			UserDO userDO = new UserDO();
			userDO.setName(user.getName());
			userDO.setDepartment(departmentEJB.mapDepartmentToDO(user.getDepartment()));
			userDO.setPassword(user.getPassword());
			userDO.setUsername(user.getUsername());
			userDO.setUserType(user.getUserType());
			userDO.setTickets(ticketEJB.mapTickets(user.getTickets()));
			userDO.setId(user.getUserId());
			return userDO;
		}else return null;
	}
}


